﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ScFramework.Core;
using System;
using System.Reflection;
using ScFramework.Utilities;
using System.Linq;
/// <summary>
/// 用于管理Entity游戏实体的加载及内存池
/// </summary>
public class EntityResource : SystemModule
{



    /// <summary>
    /// 组件对象池,使用组件上挂载的SComponent的哈希值作为键值
    /// </summary>
   // Dictionary<ComponentPrefab, List<GameObject>> componentObjectPool = new Dictionary<ComponentPrefab, List<GameObject>>();
    /// <summary>
    /// 根据组件预设生成一个各项属性初始化过的组件
    /// </summary>
    /// <param name="p"></param>
    /// <returns></returns>
  /*  GameObject CreateObject(ComponentPrefab p,List<ValueList>lists)
    {
        GameObject ans;
        if (componentObjectPool.ContainsKey(p)&&componentObjectPool[p].Count>0)
        {
            var list = componentObjectPool[p];
            ans = list.Last();
            list.RemoveAt(list.Count - 1);
        }
        else
        {
            ans = _main.Get<ResourceManager>().Instantiate("Prefab/Component/"+p.PrefabPath);
            Debug.Assert(ans.GetComponent<SComponent>() == null);
            Debug.Assert(typeof(SGameObject).IsAssignableFrom(Type.GetType(lists[0].TypeName)));
            foreach(var x in lists)
            {
                var type = Type.GetType(x.TypeName);
                Debug.Assert(typeof(SComponent).IsAssignableFrom(type));
                var com=ans.AddComponent(type) as SComponent;
            }
        }
        return ans;

    }*/
    /// <summary>
    /// 根据名称创建一个实体,初始化Init,且实体上数据与预设上设定一致,并同步data中的部分数据;同时创建data中不包含的组件的数据
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public Entity CreateEntity(EntityData data)
    {
        var prefab = EntityUtilities.GetPrefab(data.EntityIdentity.left());


        var obj= _main.Get<ResourceManager>().Instantiate(prefab);


    //    int pointer = 0;


       /* foreach(var c in prefab.ComponentValueList)
        {
            if (typeof(SGameObject).IsAssignableFrom(Type.GetType(c.TypeName)))
            {
                if (valuelists.Count>0)
                {
                    objs.Add(CreateObject(prefab.ComponentPrefabs[pointer++], valuelists));
                    valuelists.Clear();
                }
            }
            valuelists.Add(c);
        }
      

        if (valuelists.Count>0)
        {
            objs.Add(CreateObject(prefab.ComponentPrefabs[pointer++], valuelists));
            valuelists.Clear();
        }*/

      //  Debug.Assert(pointer == prefab.ComponentPrefabs.Count);

        return EntityUtilities.CreateEntityAndInit(obj,data.EntityIdentity, data.ComponentDatas,GameMode.Instance.GetMapTransform());

    }

    public void DestoryEntity(Entity e)
    {
        ObjectPool.Recycle(e.gameObject);
    }
    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="m"></param>
    public override void Initialize(Main m)
    {
        base.Initialize(m);


        //init prefabsDic from local file

    }
    public override void Dispose()
    {
      //  componentObjectPool.Clear();
     //   throw new System.NotImplementedException();
    }

    public override void FixedUpdate(float deltaTime)
    {
    //    throw new System.NotImplementedException();
    }

    public override void Update(float deltaTime)
    {
     //   throw new System.NotImplementedException();
    }
}
/*[Serializable]
/// <summary>
/// 组件预设
/// </summary>
public class ComponentPrefab
{
    /// <summary>
    /// 此组件的父物体;有父物体为父物体SGameObject的id,否则为自己id;
    /// </summary>
    public int ParentID;
    /// <summary>
    /// 组件类型
    /// </summary>
    //public Type ComponentType;

    public string PrefabPath;
}*/
/*[Serializable]
/// <summary>
/// 实体预设,由此可生成一个设定好的实体
/// </summary>
public class EntityPrefab
{
    public string EntityName;
    /// <summary>
    /// 组件预设名称
    /// </summary>
    public List<ComponentPrefab> ComponentPrefabs;
    public List<ValueList> ComponentValueList = new List<ValueList>();
}
[Serializable]
public class ValueList
{
    public string TypeName;
    public List<PrefabValuePair> Values;
}*/


